<?php
namespace PsiFramework\Applications\DefaultApp\Classes\Actions\User;

import("Classes.Actions.AbstractAction");
import("Classes.Util.HTTP.Session");

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\HTTP\Session;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The ConfirmRegistration Action
 *
 * This action is used when a new user wants to verify his account
 *
 * @package Actions.User
 */
class ConfirmRegistrationAction extends AbstractAction
{

    /**
     * Function executed by the controller
     *
     * It shows the validation form or validates an account
     *
     * @return Response    the response to the action execution
     */
    public function execute()
    {
        if ($this->_request->getParameter("activationKey") != null) {
            //if an activation key is specified
            $activationKey = $this->_request->getParameter("activationKey");

            //we try to retrieve the activation corresponding to it
            $activationDao = DAOFactory::getDAO("Activation");
            $activation = $activationDao->retrieveByPk($activationKey);

            if ($activation != null) {
                //if the activation key has been found
                $user = $activation->getUser();

                //the corresponding user is activated
                $user->setActivated(true);
                $userDao = DAOFactory::getDAO("User");
                $userDao->save($user);

                //the key is deleted from the database
                $activationDao->delete($activation);

                //the default module is replaced by
                //the result of the LoginAction
                $this->findForward("success");
            } else {
                //if the key is not in the activation table
                $this->_response->setAttribute("error", "BAD_KEY");
                $this->findForward("form");
            }
        } else {
            $this->findForward("form");
        }
    }
}
